//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.controller;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.base.BaseController;
import com.config.MinioConfig;
import com.dict.comment.FileDto;
import com.dict.comment.domain.entity.EventWaring;
import com.entry.DeviceNumber;
import com.entry.WebSocketCan;
import com.entry.YiWuAndYanHuoNumber;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.service.*;
import com.sq.common.web.base.TableDataInfo;
import com.sq.common.web.domain.AjaxResult;
import java.util.List;

import com.utils.JsonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/event/")
public class EventWaringController extends BaseController {
    @Autowired
    private EventWaringService eventWaringService;
    @Autowired
    private SanShiService sanShiService;
    @Autowired
    private JiBieService jiBieService;
    @Autowired
    private YiWuAndYanHuoNumberService yiWuAndYanHuoNumberService;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private DeviceNumberService deviceNumberService;
    @PostMapping({"selectWaring"})
    public TableDataInfo getAllEventWaring(@RequestBody EventWaring eventWaring) {
        startPage(eventWaring.getPageNum(),eventWaring.getPageSize());
        List<EventWaring> list = eventWaringService.getAllEventWaring(eventWaring);
        System.out.println("!111");
        return getDataTable(list);
    }
    @GetMapping("getone/{Id}")
    public AjaxResult getOneAlgor(@PathVariable("Id") Integer Id){
        EventWaring eventWaring = new EventWaring();
        eventWaring.setId(Id);
        EventWaring eventWaring1 = eventWaringService.getAllEventWaring(eventWaring).get(0);
      //  System.out.println("查看详情"+sysAlgor);
        return AjaxResult.success(eventWaring1);
    }

    @PostMapping("getWeb")
    public AjaxResult getWeb(@RequestBody WebSocketCan webSocketCan){
        return AjaxResult.success(eventWaringService.getWeb(webSocketCan));
    }
    @PostMapping({"test"})
    public void C(@RequestBody String string) {
        EventWaring message = JsonExample.getMessage(string);
        FileDto fileDto = new FileDto();
        fileDto.setFileName(JsonExample.getNowDate());
        fileDto.setBucketName(minioConfig.getBucketName());
        fileDto.setFileType("jpg");
        fileDto.setFilePath("");
        //获取base64数据
        fileDto.setBase64(JsonExample.extractBase64Data(message.getImgbase64()));
        System.out.println("fileDto111111:"+JSONUtil.toJsonStr(fileDto));
        //向minio发请求存储
        String post = HttpUtil.post(minioConfig.getUploadurl(), JSONUtil.toJsonStr(fileDto));
        System.out.println(post);
        //获取存储路径
        // 解析 JSON 字符串为 JsonObject
        JsonObject jsonObject = JsonParser.parseString(post).getAsJsonObject();

        // 提取 data 字段的值
        String filepath = jsonObject.get("data").getAsString();
        //拼接路径
        String realPath = minioConfig.getGetimageurl()+filepath;
        message.setImgbase64(realPath);

        Integer integer = eventWaringService.saveEventWaring(message);
        if (integer==0){
            System.out.println("成功");
        }
    }

    /**
     * 确认消息
     * @param eventWaring
     * @return
     */
    @PostMapping({"inspectWaring"})
    public AjaxResult inspectEventWaring(@RequestBody EventWaring eventWaring) {
        try {
            EventWaring eventWaring1 = eventWaringService.selectByLabel(eventWaring).get(0);
            Integer label = eventWaring1.getLabel();
            if (label==2) {
                return AjaxResult.error("消息已核查");
            }else if (label==3){
                return AjaxResult.error("消息已排除");
            }else if(label==4){
                return AjaxResult.error("消息已推送");
            }else{
                eventWaring.setState(2);
                eventWaringService.updateWaring(eventWaring);
            }
        } catch (Exception var3) {
            var3.printStackTrace();
            return AjaxResult.error("核查消息失败");
        }
        return AjaxResult.success("消息核查成功");
    }
    @PostMapping({"selectByLabel"})
    public List<EventWaring> selectByLabel(@RequestBody EventWaring eventWaring) {
        List<EventWaring> list = eventWaringService.selectByLabel(eventWaring);
        return list;
    }
    /**
     * 推送
     * @param eventWaring
     * @return
     */
    @PostMapping({"pushWaring"})
    public AjaxResult pushWaring(@RequestBody EventWaring eventWaring) {
        try {
            EventWaring eventWaring1 = eventWaringService.selectByLabel(eventWaring).get(0);
            Integer label = eventWaring1.getLabel();
            if (label==1) {
                return AjaxResult.error("消息未核查");
            }else if (label==3){
                return AjaxResult.error("消息已排除");
            }else if(label==4){
                return AjaxResult.error("消息已推送");
            }else{
                eventWaring.setState(4);
                eventWaringService.updateWaring(eventWaring);
            }
           // EventWaring eventWaring2 = new EventWaring(label);
            //推送操作


        } catch (Exception var5) {
            var5.printStackTrace();
            return AjaxResult.error("消息推送失败");
        }
        return AjaxResult.success("消息推送成功");
    }
    /**
     * 排除
     * @param eventWaring
     * @return
     */
    @PostMapping({"wipeWaring"})
    public AjaxResult wipeWaring(@RequestBody EventWaring eventWaring) {
        try {
            EventWaring eventWaring1 = eventWaringService.selectByLabel(eventWaring).get(0);
            Integer label = eventWaring1.getLabel();
            if (label==2) {
                return AjaxResult.error("消息已核查");
            }else if (label==3){
                return AjaxResult.error("消息已排除");
            }else if(label==4){
                return AjaxResult.error("消息已推送");
            }else {
                eventWaring.setState(3);
                eventWaringService.updateWaring(eventWaring);
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            return AjaxResult.error("消息排除失败");
        }
        return AjaxResult.success("消息排除成功");
    }

    @PostMapping({"getWarningTop"})
    public AjaxResult getWarningTop() {
       return AjaxResult.success(eventWaringService.getWarningTop());
    }

    @PostMapping({"getWarningTop5"})
    public AjaxResult getWarningTop5() {
        return AjaxResult.success(eventWaringService.getWarningTop5());
    }
    @GetMapping({"getDeviceNumber"})
    public AjaxResult  getDeviceNumber(){
        System.out.println(deviceNumberService.getDeviceNumber());
        return AjaxResult.success(deviceNumberService.getDeviceNumber());
    }

    @GetMapping({"getYiWuAndYanHuo"})
    public AjaxResult  getYiWuAndYanHuo(){
        return AjaxResult.success(yiWuAndYanHuoNumberService.getYiWuAndYanHuoNumber());
    }

    @GetMapping({"getJiBie"})
    public AjaxResult  getJiBie(){
        return AjaxResult.success(jiBieService.getJiBie());
    }

    @GetMapping({"getSanShi"})
    public AjaxResult  getSanShi(){
        return AjaxResult.success(sanShiService.getSanShi());
    }
}
